<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <link rel="icon" href="../picture/img1.ICO" type="image/x-icon">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>学生宿舍管理系统 - 登录</title>
    <style>

        * {  
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }

        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background: linear-gradient(135deg, #64b5f6, #1976d2);
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            color: #333;
            position: relative;
            overflow: hidden;
        }

        @keyframes float {
            0% {
                transform: translateX(0) translateY(0);
            }
            50% {
                transform: translateX(50px) translateY(-10px);
            }
            100% {
                transform: translateX(0) translateY(0);
            }
        }

        .login-container {
            background-color: white;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
            padding: 35px;
            width: 380px;
            text-align: center;
            position: relative;
            z-index: 1;
            transition: transform 0.3s ease;
        }

        .login-container:hover {
            transform: translateY(-5px);
        }

        .login-header {
            margin-bottom: 30px;
        }

        .login-header h1 {
            font-size: 26px;
            margin: 0;
            color: #1a314a;
            font-weight: 600;
            letter-spacing: 1px;
        }

        .login-header p {
            margin: 8px 0 0;
            color: #893434;
            font-size: 15px;
        }

        .form-group {
            margin-bottom: 22px;
            text-align: left;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-weight: 600;
            font-size: 15px;
            color: #444;
        }

        .form-group input {
            width: 100%;
            padding: 12px 15px;
            border: 1px solid #ddd;
            border-radius: 6px;
            font-size: 15px;
            transition: border-color 0.3s, box-shadow 0.3s;
        }

        .form-group input:focus {
            border-color: #64b5f6;
            box-shadow: 0 0 0 3px rgba(100, 181, 246, 0.2);
            outline: none;
        }

        .captcha-group {
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .captcha-group input {
            flex: 1;
        }

        .captcha-img {
            width: 90px;
            height: 42px;
            background: linear-gradient(135deg, #e3f2fd, #bbdefb);
            border-radius: 6px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: bold;
            color: #0d47a1;
            font-family: 'Arial', sans-serif;
            font-size: 20px;
            letter-spacing: 3px;
            cursor: pointer;
            user-select: none;
            transition: transform 0.2s;
        }

        .captcha-img:hover {
            transform: scale(1.05);
        }

        .user-type {
            display: flex;
            justify-content: center;
            margin: 25px 0;
            gap: 25px;
        }

        .user-type label {
            display: flex;
            align-items: center;
            cursor: pointer;
            font-size: 15px;
            color: #444;
            position: relative;
            padding-left: 25px;
        }

        .user-type input[type="radio"] {
            position: absolute;
            opacity: 0;
            cursor: pointer;
        }

        .radio-custom {
            position: absolute;
            left: 0;
            height: 18px;
            width: 18px;
            background-color: #eee;
            border-radius: 50%;
            border: 2px solid #bbb;
            transition: all 0.3s;
        }

        .user-type input[type="radio"]:checked ~ .radio-custom {
            background-color: #1976d2;
            border-color: #1976d2;
        }

        .radio-custom:after {
            content: "";
            position: absolute;
            display: none;
            top: 3px;
            left: 3px;
            width: 10px;
            height: 10px;
            border-radius: 50%;
            background: white;
        }

        .user-type input[type="radio"]:checked ~ .radio-custom:after {
            display: block;
        }

        .button-group {
            display: flex;
            justify-content: space-between;
            gap: 15px;
        }

        .button-group button {
            flex: 1;
            padding: 12px;
            border: none;
            border-radius: 6px;
            cursor: pointer;
            font-weight: 600;
            font-size: 15px;
            transition: all 0.3s;
        }

        .reset-btn {
            background-color: #ef5350;
            color: white;
        }

        .reset-btn:hover {
            background-color: #e53935;
            box-shadow: 0 4px 8px rgba(239, 83, 80, 0.3);
        }

        .login-btn {
            background-color: #1976d2;
            color: white;
        }

        .login-btn:hover {
            background-color: #1565c0;
            box-shadow: 0 4px 8px rgba(25, 118, 210, 0.3);
        }


        body {
            background: linear-gradient(to bottom, #0a0a2a, #1a1a4a);
            background-image: radial-gradient(white 1px, transparent 1px);
            background-size: 20px 20px;
        }

        body {
            background: linear-gradient(rgba(255, 255, 255, 0.7), rgba(255, 255, 255, 0.7)),
            url('https://example.com/campus.jpg');
            background-size: cover;
        }

        body {
            background: #f5f9ff;
        }

        body {
            background: #e3f2fd;
            position: relative;
            overflow: hidden;
        }

        /* 响应式设计 */
        @media (max-width: 480px) {
            .login-container {
                width: 90%;
                padding: 25px;
            }
        }

        .form-group {
            position: relative;
            margin-bottom: 30px;
        }


        .form-group label {
            position: absolute;
            left: 7px; /* 调整标签位置 */
            top: 14px;
            color: #888;
            font-weight: 400;
            transition: all 0.3s;
            pointer-events: none;
            background: white;
            padding: 0 5px;
        }

        .input-icon {
            position: absolute;
            left: 12px;
            top: 50%;
            transform: translateY(-50%);
            color: #888;
            font-size: 18px;
        }

        .form-group input:focus + label,
        .form-group input:not(:placeholder-shown) + label {
            top: -10px;
            left: 10px;
            font-size: 12px;
            color: #4a90e2;
            background: white;
        }

        @keyframes pulse {
            0% {
                transform: scale(1);
            }
            50% {
                transform: scale(1.05);
            }
            100% {
                transform: scale(1);
            }
        }

        .login-btn:hover {
            animation: pulse 0.5s ease-in-out;
        }

        #logo {
            width: 120px;
            height: auto;
            max-width: 100%;
            transition: transform 0.3s ease;
        }
    </style>
</head>
<body>
<img src="../images/enroll/【哲风壁纸】原野-月-月色.png" class="peijing"
     style="position: absolute;z-index: -1;width: 2000px;height: 1000px">
<div class="login-container">
    <div class="login-header">
        <h1>学生宿舍管理系统</h1>
    </div>
    <img src="../picture/image-removebg-preview.png" alt=""id="logo">
    <form action="<%=request.getContextPath()%>/studentLoginController?op=login" method="post">
        <div class="form-group">
            <i class="fas fa-user input-icon"></i>
            <input type="text" id="username" name="username" value="" placeholder=" " required>
            <label for="username">用户名</label>
            <div id="usernameError" class="error-message"></div>
        </div>

        <div class="form-group">
            <i class="fas fa-lock input-icon"></i>
            <input type="password" id="password" name="password" placeholder=" " required>
            <label for="password">密码</label>
            <div id="passwordError" class="error-message"></div>
        </div>

        <div class="form-group">
            <i class="fas fa-shield-alt input-icon"></i>
            <div class="captcha-group">
                <input type="text" id="captcha" name="captcha" placeholder=" " required>
                <label for="captcha">验证码</label>
                <div class="captcha-img" onclick="refreshCaptcha()">cm</div>
            </div>
            <div id="captchaError" class="error-message"></div>
        </div>

        <div class="user-type">
            <label>
                <input type="radio" name="userType" value="student">
                <span class="radio-custom"></span>
                学生
            </label>
            <label>
                <input type="radio" name="userType" value="dormitory">
                <span class="radio-custom"></span>
                宿管
            </label>
            <label>
                <input type="radio" name="userType" value="admin">
                <span class="radio-custom"></span>
                管理员
            </label>
        </div>

        <div class="button-group">
            <button type="button" class="login-btn" onclick="validateLogin()">登录</button>
            <button type="button" class="reset-btn" onclick="zuce()">注册</button>
        </div>

    </form>


    <div id="faceAuthModal"
         style="display:none;position:fixed;top:0;left:0;width:100%;height:100%;background:rgba(0,0,0,0.7);z-index:1000;justify-content:center;align-items:center;">
        <div style="background:white;padding:20px;border-radius:8px;width:80%;max-width:500px;">
            <div style="text-align:right;">
                <button id="closeFaceAuthBtn" style="background:none;border:none;font-size:20px;">×</button>
            </div>
            <video id="faceVideo" width="100%" autoplay style="display:none;"></video>
            <canvas id="faceCanvas" width="400" height="300" style="display:none;"></canvas>
            <div id="facePreview" style="text-align:center;"></div>
            <button id="captureFaceBtn" class="login-button" style="margin-top:15px;width:100%;">拍摄照片</button>
        </div>
    </div>

</div>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>


<script>

    function refreshCaptcha() {

        const chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        let captcha = "";
        for (let i = 0; i < 4; i++) {
            captcha += chars.charAt(Math.floor(Math.random() * chars.length));
        }
        const captchaImg = document.querySelector('.captcha-img');
        captchaImg.textContent = captcha;
        const captchaImg1 = document.getElementById('captcha-image');
        if(captchaImg1) {
            captchaImg1.src = '/captcha?' + new Date().getTime(); // 加时间戳避免缓存
        }
        // 添加一些干扰线或噪点的样式（需要CSS配合）
        document.querySelector('.captcha-img').textContent = captcha;
        currentCaptcha = captcha;

        // 添加简单的混淆（实际项目中应该更复杂）
        sessionStorage.setItem('captchaHash', simpleHash(captcha));
    }


    $(document).ready(function () {
        refreshCaptcha();
        let faceAuthStream = null;

// 修改拍摄照片按钮的事件处理
        document.getElementById('captureFaceBtn').addEventListener('click', function() {
            const video = document.getElementById('faceVideo');
            const canvas = document.getElementById('faceCanvas');
            const ctx = canvas.getContext('2d');
            const photoPreview = document.getElementById('facePreview');
            
            // 绘制当前视频帧到canvas
            ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
            
            // 显示拍摄的照片
            photoPreview.innerHTML = '';
            const img = document.createElement('img');
            img.src = canvas.toDataURL('image/png');
            img.style.maxWidth = '100%';
            photoPreview.appendChild(img);
            
            // 获取照片数据
            const facePhoto = canvas.toDataURL('image/png');
            
            // 关闭摄像头和模态框
            closeFaceAuthModal();
            
            // 确保照片数据有效
            if (!facePhoto) {
                alert('拍摄照片失败，请重试');
                return;
            }
            
            // 提交表单
            submitWithFacePhoto(facePhoto);
        });

// 关闭模态框函数
        function closeFaceAuthModal() {
            const modal = document.getElementById('faceAuthModal');
            const video = document.getElementById('faceVideo');
            
            if (faceAuthStream) {
                faceAuthStream.getTracks().forEach(track => track.stop());
                faceAuthStream = null;
            }
            
            modal.style.display = 'none';
            video.style.display = 'none';
        }
    });




    // 更复杂的验证码生成

    // 简单的哈希函数（仅示例，实际应该使用更安全的哈希）
    function simpleHash(str) {
        let hash = 0;
        for (let i = 0; i < str.length; i++) {
            hash = ((hash << 5) - hash) + str.charCodeAt(i);
            hash |= 0; // 转换为32位整数
        }
        return hash;
    }

    function zuce() {
        window.open("enroll.jsp")
    }

    function validateLogin() {
        let isValid = true;
        // 获取登录按钮元素
        const loginBtn = document.querySelector('.login-btn');
    
        // 清除所有错误信息
        document.querySelectorAll('.error-message').forEach(el => {
            el.textContent = '';
            el.style.display = 'none';
        });
    
        // 验证用户名、密码和验证码
        const username = document.getElementById('username').value.trim();
        const password = document.getElementById('password').value;
        const captcha = document.getElementById('captcha').value.trim();
        const userType = document.querySelector('input[name="userType"]:checked').value;
    
        if (!username) {
            showError('usernameError', '请输入用户名');
            isValid = false;
        }
        if (!password) {
            showError('passwordError', '请输入密码');
            isValid = false;
        }
        if (!captcha) {
            showError('captchaError', '请输入验证码');
            isValid = false;
        }
    
        if (isValid) {
            // 显示加载状态
            loginBtn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> 正在登录...';
            loginBtn.disabled = true;
    
            if (userType === 'admin') {
                // 管理员直接提交表单
                document.querySelector('form').submit();
            } else {
                // 其他用户显示人脸认证模态框
                showFaceAuthModal();
            }
        }
    
        return false;
    }


    // 确保showFaceAuthModal函数正确显示模态框和摄像头
    function showFaceAuthModal() {
        const modal = document.getElementById('faceAuthModal');
        const video = document.getElementById('faceVideo');

        modal.style.display = 'flex';
        video.style.display = 'block';

        // 打开摄像头
        navigator.mediaDevices.getUserMedia({video: true})
            .then(function (stream) {
                faceAuthStream = stream;
                video.srcObject = stream;
            })
            .catch(function (err) {
                console.error('摄像头访问错误:', err);
                alert('无法访问摄像头: ' + err.message);
                modal.style.display = 'none';
            });
    }


    function submitWithFacePhoto(facePhoto) {
        if (!facePhoto) {
            alert('请先拍摄人脸照片');
            return;
        }
        
        const form = document.querySelector('form');
        const btn = document.querySelector('.login-btn');
        
        // 创建隐藏input来传递照片数据
        let faceInput = document.createElement('input');
        faceInput.type = 'hidden';
        faceInput.name = 'facePhoto';
        faceInput.value = facePhoto;
        form.appendChild(faceInput);
        
        // 显示加载状态
        btn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> 登录中...';
        btn.disabled = true;
        
        // 提交表单
        form.submit();
    }

    function showError(id, message) {
        const errorEl = document.getElementById(id);
        errorEl.textContent = message;
        errorEl.style.display = 'block';
    }

    <% 
    String file = (String) session.getAttribute("file");
    if(file != null) { 
    %>
        alert('<%=file%>');
    <% 
    } 
    %>
</script>
</body>
</html>

